#include "Object.h"
using namespace std;

Object::Object(bool anc)
{
    anchored = anc;
    orient = UP;
}

Object::Object(int x, int y, std::string newType, Direction orientation)
{
    orient = orientation;
    bounds.x = x;
    bounds.y = y;
    type = newType;

    initFromFile();
}

Object::~Object()
{
}

SDL_Rect Object::get_bounds_copy()
{
    SDL_Rect obj_bounds = img.get_bounds();
    obj_bounds.x = bounds.x;
    obj_bounds.y = bounds.y;

    return obj_bounds;
}

SDL_Rect* Object::get_bounds()
{
    return &bounds;
}

void Object::rotate(Artist& artist, Direction dir, bool rotate_flipped)
{
    switch(dir)
    {
        case RIGHT: dir = LEFT;
                   if(orient == UP)
                        orient = LEFT;
                   else if(orient == DOWN)
                        orient = RIGHT;
                   else if (orient == LEFT)
                        orient = DOWN;
                   else if (orient == RIGHT)
                        orient = UP;
                    break;
        case LEFT:  dir = RIGHT;
                   if(orient == UP)
                        orient = RIGHT;
                   else if(orient == DOWN)
                        orient = LEFT;
                   else if (orient == LEFT)
                        orient = UP;
                   else if (orient == RIGHT)
                        orient = DOWN;
                    break;
        case UP:
        case DOWN: if(orient == UP)
                        orient = DOWN;
                   else if(orient == DOWN)
                        orient = UP;
                   else if (orient == LEFT)
                        orient = RIGHT;
                   else if (orient == RIGHT)
                        orient = LEFT;
                    break;
    }
    img.rotateSprite(artist, dir, rotate_flipped);
}

ObjectInfo Object::get_info()
{
    ObjectInfo info = {type, bounds.x, bounds.y, orient};
    return info;
}

void Object::set_image(Artist& artist, std::string filepath)
{
    if(filepath == "" && type!="")
        filepath = "object/" + type + ".png";

    img.setImage(artist, filepath, false, true);

    if(orient != UP)
        img.rotateSprite(artist, orient);

    bounds.w = img.get_bounds().w;
    bounds.h = img.get_bounds().h;
}

void Object::display(SDL_Rect camera, SDL_Surface* screen, bool lock_position)
{
    if(!halfspeedscroll || lock_position)
        img.display(screen, false, bounds.x - camera.x, bounds.y - camera.y);
    else
        img.display(screen, false, bounds.x - camera.x + (camera.x*2/3), bounds.y - camera.y + (camera.y*2/3));
}

void Object::initFromFile()
{
    string filepath = "data/objects/" + type + ".obj";

	cout << "Loading object " << filepath << "\n";

	ifstream objectFile(filepath.c_str(), std::ifstream::in);

	if(objectFile != NULL)
	{
		cout << "ifstream successfull, loading object.\n";

        string tag = "";

        while(getline(objectFile, tag))
        {
            if(tag == "SIZE")
            {
                objectFile >> bounds.w;
                bounds.h = bounds.w;
                cout << "size set to " << bounds.w << ", " << bounds.h << "\n";
            }
            else if (tag == "DAMAGE")
            {
                objectFile >> damage;
                cout << "damage set to " << damage << "\n";
            }
            else if (tag == "ANCHORED")
            {
                string anc;
                objectFile >> anc;

                if (anc == "distant" || anc == "distance")
                    halfspeedscroll = true;
                else
                    halfspeedscroll = false;

                if(anc == "true" || anc == "yes" || anc == "distant" || anc == "distance")
                    anchored = true;
                else if(anc == "false" || anc == "no")
                    anchored = false;

                cout << "anchor set to " << anchored << "\n";
            }
            else if (tag[0] = '#' || tag == "" || tag == " "){
            }else
                cout << "skipping unknown tag \"" << tag << "\"\n\n";
        }
	}
}
